<template>
  <div>
    <el-card class="app-container">
      <c-u-r-d-table :option="option" ref="table"/>
    </el-card>
    <dialog-columns-form
      v-model="formData"
      width="600px"
      title="结果处理"
      :visible.sync="resultDialogShowed"
      :columns="resultColumns"
      @submit="handleSubmitResultStatus"
    />
  </div>

</template>

<script>

import ColumnType from '@/framework/constant/ColumnType'
import { expressLostApi } from '@/feature/cmt/infra/api/expressLostApi'
import CURDTable from '@/framework/components/table/CURDTable'
import ExpressLostTypeEnum from '@/feature/cmt/infra/constants/ExpressLostTypeEnum'
import { storeApi } from '@/feature/mct/infra/api/storeApi'
import { adminApi } from '@/feature/mgr/infra/api/adminApi'
import DateType from '@/framework/constant/DateType'
import ExpressLossPaidStatusEnum from '@/feature/cmt/infra/constants/ExpressLossPaidStatusEnum'
import YesNoEnum from '@/infra/constant/YesNoEnum'
import ExpressLossReceiveFlagEnum from '@/feature/cmt/infra/constants/ExpressLossReceiveFlagEnum'
import ExpressLossFinishFlagEnum from '@/feature/cmt/infra/constants/ExpressLossFinishFlagEnum'
import ExpressLossResultStatusEnum from '@/feature/cmt/infra/constants/ExpressLossResultStatusEnum'
import DialogColumnsForm from '@/framework/components/form/DialogColumnsForm'
import ExpressLossTypeEnum from '@/feature/cmt/infra/constants/ExpressLossTypeEnum'

export default {
  components: { DialogColumnsForm, CURDTable },
  data() {
    return {
      option: {
        requestApi: expressLostApi,
        showSelection: false,
        opWidth: 180,
        columns: [
          {
            label: '登记日期',
            hiddenInForm: true,
            searchable: true,
            width: 90,
            prop: 'registerDate',
            type: ColumnType.DATE,
            searchOption: {
              options: {
                type: DateType.DATERANGE
              }
            }
          },
          {

            label: '旺旺号',
            prop: 'wwId',
            searchable: true,
            type: ColumnType.TEXT
          },
          {
            label: '店铺',
            prop: 'storeId',
            searchable: true,
            type: ColumnType.SELECT,
            required: true,
            options: storeApi.options()
          },
          {
            label: '快递公司',
            required: true,
            width: 100,
            type: ColumnType.SELECT,
            searchable: true,
            prop: 'expressComName',
            tagMode: false,
            options: ['申通', '中通', '邮政（EMS）', '顺丰', '韵达'].map(el => ({ label: el, value: el }))
          },
          {
            label: '快递单号',
            required: true,
            width: 'auto',
            searchable: true,
            prop: 'expressNo'
          }, {
            label: '遗失类型',
            required: true,
            width: 100,
            type: ColumnType.SELECT,
            searchable: true,
            options: ExpressLossTypeEnum,
            prop: 'lossType'
          },

          {
            label: '订单金额',
            required: true,
            width: 100,
            type: ColumnType.MONEY,
            prop: 'expressMoney'
          },
          {
            // label: '类型：0-补发，1-退款',
            label: '类型',
            searchable: true,
            width: 100,
            required: true,
            prop: 'type',
            type: ColumnType.SELECT,
            options: ExpressLostTypeEnum
          },
          {
            label: '补发订单号',
            required: true,
            width: 120,
            showFilter: ({ value }) => value.type === ExpressLostTypeEnum.SUPPLY_AGAIN.id,
            searchable: true,
            prop: 'supplyAgainExpressNo'
          },
          {
            label: '结果状态',
            searchable: true,
            width: 100,
            hiddenInForm: true,
            required: true,
            prop: 'resultStatus',
            type: ColumnType.SELECT,
            options: ExpressLossResultStatusEnum
          }, {
            label: '是否已完结',
            searchable: true,
            width: 100,
            hiddenInForm: true,
            required: true,
            prop: 'finishFlag',
            type: ColumnType.SELECT,
            options: ExpressLossFinishFlagEnum
          },
          {
            label: '备注',
            searchable: true,
            width: 100,
            hiddenInForm: true,
            prop: 'notes'
          },
          {
            label: '登记人',
            prop: 'registerById',
            searchable: true,
            type: ColumnType.SELECT,
            width: 100,
            options: adminApi.listAll(),
            hiddenInForm: true
          },
          {
            label: '创建时间',
            type: ColumnType.DATETIME,
            hiddenInForm: true,
            prop: 'gmtCreate'
          },
          {
            label: '修改时间',
            type: ColumnType.DATETIME,
            hiddenInForm: true,
            prop: 'gmtModified'
          }
        ]
      },
      resultDialogShowed: false,
      formData: {
        notes: '',
        id: '',
        resultStatus: ''
      }
    }
  },
  computed: {
    resultColumns() {
      return [
        {
          label: '结果状态',
          prop: 'resultStatus',
          span: 24,
          required: true,
          type: ColumnType.SELECT,
          options: ExpressLossResultStatusEnum.getOptions().filter(el => el.value !== ExpressLossResultStatusEnum.NO.id)
        },
        {
          label: '备注',
          prop: 'notes',
          span: 24,
          required: this.formData.resultStatus === ExpressLossResultStatusEnum.OTHER.id,
          type: ColumnType.TEXTAREA
        }
      ]
    }
  },
  methods: {
    handleUpdatePaidStatus({ arg: row }) {
      this.$confirm('确认已赔付？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        expressLostApi.updatePaidStatus(row.id, 1).then(() => {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.$refs.table.reloadTable()
        })
      })
    },
    handleUpdateResultStatus({ arg: row }) {
      this.formData.id = row.id
      this.formData.resultStatus = ''
      this.formData.notes = ''
      this.resultDialogShowed = true
    },
    handleUpdateReceiveFlag({ arg: row }) {
      this.$confirm('确认已收货？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        expressLostApi.updateReceiveFlag(row.id, 1).then(() => {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.$refs.table.reloadTable()
        })
      })
    },
    handleSubmitResultStatus({ done }) {
      expressLostApi.updateResultStatus(this.formData).then(() => {
        this.$message({
          type: 'success',
          message: '操作成功!'
        })
        this.$refs.table.reloadTable()
        this.resultDialogShowed = false
      }).finally(() => {
        done()
      })
    }
  }
}
</script>

